package com.yonyou.pmclouds.statistic.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.alter.rmiitf.AlterQuery;
import com.yonyou.pmclouds.alterandvisa.rmiitf.AlterAndVisaQuery;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.SqlUtil;
import com.yonyou.pmclouds.organization.entity.OrganizationVO;
import com.yonyou.pmclouds.organization.mapper.OrganizationMapper;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.reason.rmiitf.ProblemReasonQuery;
import com.yonyou.pmclouds.statistic.entity.DataReportVO;
import com.yonyou.pmclouds.statistic.entity.ProReasonCountVO;
import com.yonyou.pmclouds.statistic.mapper.ProblemReasonAnalyseMapper;
import com.yonyou.pmclouds.statistic.rmiitf.DataAnalyseRemoteService;
import com.yonyou.pmclouds.statistic.rmiitf.ProblemReasonAnalyseRemoteService;
import com.yonyou.pmclouds.team.rmiitf.ProjectRefWithTeamQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = DataAnalyseRemoteService.class)
public class DataAnalyseRemoteServiceImpl implements DataAnalyseRemoteService {


    @Autowired
    private ProblemReasonAnalyseMapper mapper;
    @Autowired
    private OrganizationMapper orgMapper;
    @Autowired
    private ProblemReasonQuery reasonQuery;
    @Autowired
    private ProjectRefWithTeamQuery projectRefQuery;
    @Autowired
    private ProjectSimpleQuery projectSimpleQuery;
    @Autowired
    private ProblemReasonAnalyseRemoteService problemReasonAnalyseRemoteService;
    @Autowired
    private AlterQuery alterQuery;
    @Autowired
    private AlterAndVisaQuery alterAndVisaQuery;


    @Override
    public DataReportVO queryDataAnalyse(String pkTenant, String pkSrcOrg, String startDate,
                                         String endDate) throws BusinessException {
        DataReportVO dataReportVO = new DataReportVO();
        Map<String, List<ProjectVO>> projectMap = projectSimpleQuery.queryProjectsByOrgs(pkTenant, new String[]{pkSrcOrg});

        if (projectMap.get(pkSrcOrg) == null || projectMap.get(pkSrcOrg).isEmpty()) {
            dataReportVO.setAlterCount(0);
            dataReportVO.setVisaCount(0);
            dataReportVO.setReasonCountList(null);
            return dataReportVO;
        }

        List<String> projectList = new ArrayList<String>();
        for (ProjectVO projectVO : projectMap.get(pkSrcOrg)) {
            projectList.add(projectVO.getPkProject());
        }
        if(!projectList.isEmpty()){
            int alterCount = alterQuery.countByCondition(pkTenant, projectList.toArray(new String[0]), null, startDate, endDate, new int[]{BillStatusConst.COMMON_APPROVED_STATUS});
            dataReportVO.setAlterCount(alterCount);

            int visaCount = alterAndVisaQuery.countByCondition(pkTenant, projectList.toArray(new String[0]), null, startDate, endDate, new int[]{BillStatusConst.COMMON_APPROVED_STATUS});
            dataReportVO.setVisaCount(visaCount);

            List<ProReasonCountVO> reasonCountList = problemReasonAnalyseRemoteService.queryProblemReasonAnalyse(pkTenant, projectList.toArray(new String[0]), startDate, endDate);
            dataReportVO.setReasonCountList(reasonCountList);
        }
        return dataReportVO;
    }

}
